﻿Ext.define("PSI.MainMenu.Admin.MenuField", {
    extend: "Ext.form.field.Trigger",
    alias: "widget.psi_main_admin_menufield",

    initComponent: function () {
        this.editable = false;
        this.enableKeyEvents = true;

        this.callParent(arguments);

        this.on("keydown", function (field, e) {
            if (e.getKey() == e.BACKSPACE) {
                field.setValue(null);
                e.preventDefault();
                return false;
            }

            if (e.getKey() != e.ENTER && !e.isSpecialKey(e.getKey())) {
                this.onTriggerClick(e);
            }
        });
    },

    onTriggerClick: function (e) {

        var me = this;
        debugger;   var modelName = "PSI_BC_Vsl";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["id", "caption", "expandable", "expanded", "leaf", "children", "fid"]
        });

        var store = Ext.create("Ext.data.TreeStore", {
            model: modelName,
            proxy: {
                type: "ajax",
                actionMethods: { read: 'POST' },
                url: "/MainMenuAdmin/GetTreeItem"
            },
            autoLoad: true,
        });
        var lookupGrid = Ext.create("Ext.tree.Panel", {
            id: "menutrees",
            border: false,
            rootVisible: false,                 // 这个比较重要  隐藏根节点
            useArrows: true,                    //树结构显示风格
          //  hideHeaders: true,                  //隐藏标题
            renderTo: Ext.getBody(),
            store: store,
            columns: {
                items: [{
                    xtype: 'treecolumn',
                    dataIndex: 'caption',
                    text: "菜单名", 
                    flex: 1
                }]
            }
        });
        me.lookupGrid = lookupGrid;
        me.lookupGrid.on("itemdblclick", me.onOK, me);

        var wnd = Ext.create("Ext.window.Window", {
            title: "选择 - 上级菜单",
            modal: true,
            width: 400,
            height: 300,
            layout: "border",
            items: [
                {
                    region: "center",
                    xtype: "panel",
                    layout: "fit",
                    border: 0,
                    items: [lookupGrid]
                }
            ],
            buttons: [
                {
                    text: "确定", handler: me.onOK, scope: me
                },
                {
                    text: "取消", handler: function () { wnd.close(); }
                }
            ]
        });
        wnd.on("close", function () {
            me.focus();
        });
        me.wnd = wnd;
        wnd.show();
    },

    // private
    onOK: function () {
        var me = this;
        var grid = me.lookupGrid;
        var item = grid.getSelectionModel().getSelection();
        if (item == null || item.length != 1) {
            return;
        }

        var data = item[0].getData();

        me.wnd.close();
        me.focus();
        me.setValue(data.caption);
        Ext.getCmp("parentids").setValue(data.id);
        me.focus();
    },

    onAdd: function () {
        Ext.getCmp("editName").focus();

        var form = Ext.create("PSI.Vsl.EditForm");
        form.show(null, null, "other");
    }
});